/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React from 'react';
import { Form, Input, Button } from 'antd';
import type { RealnameBillIndexType } from '../types/RealnameBill';
import type { ConnectState } from '@/models/connect';

import { connect } from 'umi';
import { getRanString, RealnameBillColumns } from './utils';
import CommonTable from './_common/CommonTable';
import CommonResetButton from './_common/CommonResetButton';
const FormItem = Form.Item;

interface FormType {
  waybill: string;
}

const RealnameBill = (props: RealnameBillIndexType) => {
  const { screenSize, dispatch, realnameBillData } = props;
  const [form] = Form.useForm();
  const onFinish = (values: FormType) => {
    dispatch({
      type: 'customerSearch/getWayBillSearch',
      payload: { ...values },
    });
  };

  const onReset = () => {
    form.resetFields();
    dispatch({
      type: 'customerSearch/save',
      payload: { realnameBillData: {} },
    });
  };

  return (
    <>
      <Form
        form={form}
        onFinish={onFinish}
        layout={screenSize ? 'vertical' : 'inline'}
        style={{ marginBottom: 20 }}
      >
        <FormItem
          label={'运单号'}
          name='waybill'
          rules={[{ required: true, message: '请输入运单号' }]}
        >
          <Input allowClear style={{ width: 250 }} placeholder={'请输入运单号'} />
        </FormItem>

        <FormItem>
          <Button type='primary' htmlType='submit' loading={false}>
            查询
          </Button>
          <CommonResetButton onReset={onReset} />
        </FormItem>
      </Form>
      {RealnameBillColumns.map((item) => (
        <CommonTable
          key={getRanString()}
          title={item.title}
          columns={item.columns}
          datasource={realnameBillData ? [{ ...realnameBillData, id: '1' }] : []}
        />
      ))}
    </>
  );
};

export default connect(({ customerSearch, loading }: ConnectState) => ({
  realnameBillData: customerSearch.realnameBillData || {},
  tableLoading: loading.effects['customerSearch/getWayBillSearch'] || false,
}))(React.memo(RealnameBill));
